var vehicleInfos = [];
var branchCompanyData = [];
var chargingStationData = [];
var teamData = [];
var lineData = [];
var batteryTypeData = [];
$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("vehicleInfoThead", vehicleInfoTable, search);
	scrollTableSuspend("scrollBody", "vehicleInfoTable", "vehicleInfoTable1", "vehicleInfoThead", "scrollHeight");
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	$ajaxList('branchCompany', getBranchCompany);
	setTimeout(firstSearch, 500);
})
function firstSearch(){
	var passValuesNum = window.parent.document.getElementById("passValue").value;
    if(passValuesNum != ""){
    	 $("#CarNumber").val(passValuesNum);
    	 window.parent.document.getElementById("passValue").value = '';
    }
	search();
}
function resetSearch(){
	$("#changeLine").hide();
	$("#changeTeam").hide();
	$("#changeSub").hide();
	setTimeout(search, 500);
}
function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	for (var i = 0; i < length; i++) {
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function getBranchCompany(data) {
	var length = data.length;
	branchCompanyData = data;
	for (var i = 0;i < length; i++){
    	branchCompanyData = data;
        $("#BranchCompanyID").append("<option value='" + data[i].branchCompanyID + "'>" + data[i].branchCompanyName + "</option>");
    }
}
function changeSubBranchCompany(){
	var branchCompanyID = $("#BranchCompanyID").val();
	if (branchCompanyID) {
		$("#changeSub").show();
		$("#SubBranchCompanyID").empty();
		$("#SubBranchCompanyID").append("<option value=''>请选择</option>");
		$ajaxList('subBranchCompany', getSubBranchCompany, {"branchCompanyID": branchCompanyID});
	} else {
		$("#changeSub").hide();
		$("#SubBranchCompanyID").empty();
		$("#changeTeam").hide();
		$("#TeamID").empty();
		$("#changeLine").hide();
		$("#LineID").empty();
	}
}
function getSubBranchCompany(data) {
	$("#SubBranchCompanyID").empty();
	$("#SubBranchCompanyID").append("<option value=''>请选择</option>");
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#SubBranchCompanyID").append("<option value='" + data[i].subBranchCompanyID + "'>" + data[i].subBranchCompanyName + "</option>");
	}
}
function changeCarTeam(){
	var subBranchCompanyID = $("#SubBranchCompanyID").val();
	if (subBranchCompanyID) {
		$("#changeTeam").show();
		$("#TeamID").empty();
		$("#TeamID").append("<option value=''>请选择</option>");
		$ajaxList('carTeam', getCarTeam, {"subBranchCompanyID": subBranchCompanyID});
	} else {
		$("#changeTeam").hide();
		$("#TeamID").empty();
		$("#changeLine").hide();
		$("#LineID").empty();
	}
}
function getCarTeam(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#TeamID").append("<option value='" + data[i].teamID + "'>" + data[i].teamName + "</option>");
	}
}
function changeLine(){
	var teamID = $("#TeamID").val();
	var subBranchCompanyID = $("#SubBranchCompanyID").val();
	if (teamID) {
		$("#changeLine").show();
		$("#LineID").empty();
		$("#LineID").append("<option value=''>请选择</option>");
		$ajaxList('lineName', getLineName,{"teamID": teamID, "subBranchCompanyID": subBranchCompanyID});
	} else {
		$("#changeLine").hide();
		$("#LineID").empty();
	}
}
function getLineName(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#LineID").append("<option value='" + data[i].lineID + "'>" + data[i].lineName + "</option>");
	}
}
function search(){
	$ajax('searchVehicleInfo', 'vehicleSearch', searchVehicleInfo);
}
function searchVehicleInfo(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	vehicleInfos = data && data.carInfoList || [];
	var length = vehicleInfos.length;
	createTableBody("vehicleInfoTbody", vehicleInfoTable, vehicleInfos);
	setTimeout(hideLoading, 500);
}
function details(index){
	window.parent.detalisSkip('vehicle/vehicleInfoDetail',index,$("#currentPage").val());
	$("#detailID", parent.document).val(vehicleInfos[index].carNumber);
}
function chkBefore(index, chkFlag){
	var checkbox =document.getElementsByName("chk"); 
	for (var i=0; i<checkbox.length; i++) {
		checkbox[i].checked = false; 
	}
	checkbox[index].checked = true; 
	if (chkFlag == "update") {
		update(index);
	} else if(chkFlag == "delete") {
		del();
	} else if(chkFlag == "updatePwd") {
		updatePassword(index);
	}
}
function save() {
	window.parent.skipSecondMenu('vehicle/vehicleInfoSave', 'vehicleInfo', 'carPage');
}
function update(index) {
	window.parent.detalisSkip('vehicle/vehicleInfoUpdate',index,$("#currentPage").val());
	$("#parentSkipMenu", parent.document).val("carPage");
	$("#detailID", parent.document).val(vehicleInfos[index].carNumber);
}
function confirm(){
	return checkForm("saveForm", vehicleInfoTable);
}
function getUrl(){
	return 'deleteVehicleInfo';
}
function getData(){
	return {carVin: getCheckDataList(vehicleInfos, "carVin")};
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
    $ajax('searchVehicleInfo', 'vehicleSearch', searchVehicleInfo);
}